|
DHTMLX Documentation |
<rows>After that we can add the following code to grid initialization:
<row id="1"><cell> data 2 </cell></row>
<row id="2"><cell> data 2 </cell>
<userdata name="drag">allow</userdata>
</row>
</rows>
grid.attachEvent("onBeforeDrag",function(id){From now on each time d-n-d is started, user data of the dragged item will be checked, and d-n-d will be allowed only if it is set in XML.
if (grid.getUserData(id,"drag")=="allow") return true; //allow drag if user data exists
return false; //deny drag for any other cases
}
grid.rowToDragElement=function(id){In the above mentioned snippet the text of the dragged element will contain a fixed image and the value of the first column of the dragged row. As you can see it is possible to use any HTML inside the dragged text, so it is rather customizable.
//any custom logic here
var text = "<img src='some.gif'> - "+grid.cells(id,0).getValue(); //prepare text string
return text;
}
grid.rowToDragElement=function(id){If you want to show the text of all the dragged items in case of d-n-d with multiselect it is possible to define the custom function as follows:
//any custom logic here
var text = grid._dragged.length + "item(s)";
return text;
}
grid.rowToDragElement=function(id){
//any custom logic here
var text="";
for (var i=0; i<this._dragged.length; i++)
text += grid.cells(grid._dragged[i].idd,0).getValue() + "<br/>";
return text;
}
var last_marker = null;This snippet will be called each time when any item is dragged. It sets background color to red when the dragged item enters the borders of any possible landing, and clears background color after the item is moved outside landing's borders.
mygrid.attachEvent("onDragIn",function(sid,tid,sgrid,tgrid){
// sid - id of dragged item , tid - id of landing item
if (tid) // tid may be null if landing is in grid body
grid.setRowTextStyle(tid,"background-color:red;"); // mark current landing
return true; // mandatory! important!
})
mygrid.attachEvent("onDragOut",function(tid){
if (tid)
grid.setRowTextStyle(tid,""); // clear styles which were set on the previous step
})
<rows>We can implement "category" and "item" logic (i.e. item can be dropped only in category) in the following way:
<row id="1"><cell> data 1 </cell>
<userdata name="drag">category</userdata></row>
<row id="2"><cell> data 2</cell>
<userdata name="drag">category</userdata></row>
<row id="3"><cell> data 3</cell>
<userdata name="drag">item</userdata></row>
<row id="4"><cell> data 4</cell>
<userdata name="drag">item</userdata></row>
</rows>
grid.attachEvent("onDragIn",function(sid,tid){To block the operation we just return "false" from onDragIn event.
if (grid.getUserData(tid,"drag")=="item") return false; // nothing can be dropped in an item, block landing
return true; // in any other cases - allow landing
});
mygrid.attachEvent("onDrag",function(sid,tid){
if (!some_check(sid,tid)) return false; //block d-n-d
return true;
});
mygrid.attachEvent("onDrag",function(sid,tid){The dragContext object is available during the onDrag event and it contains the following properties:
if (!some_check(sid,tid)) mygrid.dragContext.mode="copy"; // copy item instead of moving it
return true;
});
mygrid.attachEvent("onDrag",function(sid,tid){
some_custom_code(sid,tid);
return false;
});
mygrid.attachEvent("onDrag",function(sid,tid){Data conversion is required when d-n-d process occurs between different grids. This can be done by redefining gridToGrid method:
mygrid.moveRowTo(sid,someID,"copy","sibling"); //moving an item as a sibling of some other element
return false; // block default d-n-d
});
grid.gridToGrid = function(rowId,sgrid,tgrid){This snippet just copies the data, without applying any modifications to it. In a real application it may change data order or add|delete some of it.
var z=[];
for (var i=0; i<sgrid.getColumCount(); i++) // for each cell in the source grid
z[i]=sgrid.cells(rowId,i).getValue(); // prepare data for target grid
return z;
}
grid.gridToTreeElement = function(tree,treeID,gridID){
return this.cells(gridId,0).getValue(); // take data from the first column as a value for tree
}
grid.treeToGridElement = function(tree,treeID,gridID){
var z=[treeObj.getItemText(treeID)]; //set the tree text as a value of the first column in the grid
return z;
}